package com.cqs.example.io.serial;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * writeReplace作用是替换序列化的对象，且会使用writeObject进行序列化
 * writeObject改写JDK的默认序列化实现
 */
@Slf4j
@Data
public class WriteObjectSerialBean implements Serializable {
    private String name = "测试";

    /**
     * Write the map out using a custom routine.
     */
    private void writeObject(ObjectOutputStream out) throws IOException {
        log.info("调用了SingletonSerial　writeObject" + this.name);
        out.defaultWriteObject();
    }

    private Object writeReplace() throws ObjectStreamException {
        WriteObjectSerialBean bean = new WriteObjectSerialBean();
        bean.name = "writeReplace测试";
        log.info("调用了SingletonSerial　writeReplace " + this.name + "\t" + bean.name);
        return bean;
    }
}